package com.tsingteng.cosfun.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.util.AttributeSet;


import com.tsingteng.cosfun.R;

/**
 * Created by xiaoloi on 2018/8/20.
 */

public class PressImageView  extends android.support.v7.widget.AppCompatImageView{
    public PressImageView(Context context, AttributeSet attrs, int defStyle) {

        super(context, attrs, defStyle);

        init(context, attrs);

    }

    public PressImageView(Context context, AttributeSet attrs) {

        this(context, attrs, 0);

    }



    private void init(Context context, AttributeSet attrs) {

        //获取到自定义属性

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PressImageView);

        Drawable src_n = a.getDrawable(R.styleable.PressImageView_src_n);

        Drawable src_s = a.getDrawable(R.styleable.PressImageView_src_s);

        a.recycle();



        setImageDrawable(createDrawable(src_s, src_n));

    }



    public PressImageView(Context context) {

        this(context, null);

    }

    //创建drawable

    public Drawable createDrawable(Drawable press, Drawable normal) {

        StateListDrawable stateList = new StateListDrawable();

        int statePressed = android.R.attr.state_pressed;

        stateList.addState(new int[] { statePressed }, press);

        stateList.addState(new int[] {}, normal);

        return stateList;

    }
}
